package com.dianning.fio.imp;

import java.io.*;

import org.apache.commons.io.*;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.dianning.comm.Constant;
import com.dianning.comm.DianningUtil;
import com.dianning.comm.ImgUtil;
import com.dianning.fio.AvatarUploadFile;
import com.dianning.fio.InfoPicUploadFile;
import com.dianning.fio.UploadFile;

public class InfoPicUploadFileImp implements InfoPicUploadFile {
	private static final Log logger = LogFactory.getLog(InfoPicUploadFileImp.class);	

  public InfoPicUploadFileImp() {
  }

  /**
  *
  * @param toFileName String
  * @param uploadFile UploadFile
  * @param sysConfig SysConfig
  * @throws IOException
  */
 public String saveUploadFile(UploadFile uploadFile) throws IOException {
	 	String basePath = DianningUtil.mkDirByNow(Constant.INFO_PIC_PATH);
	 	String ext = FilenameUtils.getExtension(uploadFile.getFileName());
		String formjpg =basePath + "/" +  System.currentTimeMillis() + RandomStringUtils.randomNumeric(4) + "." + ext;
		OutputStream bos = new FileOutputStream(Constant.ROOTPATH + "/" + formjpg);
		IOUtils.copy(uploadFile.getInputStream(), bos);
		bos.close();
		uploadFile = null;
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			logger.error(e);
			e.printStackTrace();
		}
		//ImgUtil.reduceImg(Constant.ROOTPATH + "/" + formjpg, Constant.ROOTPATH + "/" + formjpg,Constant.ROOTPATH + "/images/sy.jpg",600,400,110,52);	//jmagick版
		ImgUtil.reduceImg(Constant.ROOTPATH + "/" + formjpg, Constant.ROOTPATH + "/" + formjpg,600,400);
		return formjpg;
 }
 
 public boolean saveAvatar(String picpath,long id){
	 DianningUtil.mkDirById(id,Constant.INFO_AVATAR_PATH);
		String[] dir = DianningUtil.getDirById(id);
		//小图
		String smalljpg = Constant.ROOTPATH + "/" + Constant.INFO_AVATAR_PATH + "/" + dir[0] + "/" + dir[1] + "/" + dir[2] + "/" + dir[3] + "_small.jpg";
		//ImgUtil.reduceImgNoLogo(Constant.ROOTPATH + "/" + picpath, smalljpg,100,100);	//jmagick版
		ImgUtil.reduceImg(Constant.ROOTPATH + "/" + picpath, smalljpg,100,100);
		//大图
		String bigjpg = Constant.ROOTPATH + "/" + Constant.INFO_AVATAR_PATH + "/" + dir[0] + "/" + dir[1] + "/" + dir[2] + "/" + dir[3] + "_big.jpg";
		//ImgUtil.reduceImgNoLogo(Constant.ROOTPATH + "/" + picpath, bigjpg,165,200);	//jmagick版
		ImgUtil.reduceImg(Constant.ROOTPATH + "/" + picpath, bigjpg,165,200);
	 return true;
 }
 
 public String findAvatar(long id,long uid,String size){
	 String[] dir = DianningUtil.getDirById(id);
	 String s = dir[0] + "/" + dir[1] + "/" + dir[2] + "/" + dir[3] + "_" + size + ".jpg";
	 File f = new File(Constant.ROOTPATH + "/" + Constant.INFO_AVATAR_PATH + "/" + s);
	 if(f.isFile()){
		 return Constant.DOMAIN + "/" + Constant.INFO_AVATAR_PATH + "/" + s;
	 }else{
		 dir = DianningUtil.getDirById(uid);
		 String as = dir[0] + "/" + dir[1] + "/" + dir[2] + "/" + dir[3] + ".jpg";
		 File af = new File(Constant.ROOTPATH + "/" + Constant.AVATAR_PATH + "/" + as);
		 if(af.isFile()){
			 return Constant.DOMAIN + "/" + Constant.AVATAR_PATH + "/" + as;
		 }
	 }
	 return Constant.DOMAIN + "/" + Constant.DEFAULT_AVATAR_PATH;
 }
 
 public static void main(String[] args) throws Exception {
	 //ImgUtil.reduceImg("d:/form.jpg", "d:/to.jpg","d:/sy.png",600,600,180,80);
	 //ImgUtil.reduceImgNoLogo("d:/form.jpg", "d:/small.jpg",100,100);
	 File f = new File("d:/small.jpg");
	 if(f.isFile()){
		 System.out.println("yes");
	 }else{
		 System.out.println("no");
	 }
 }
}
